<?php
/**
 * @author: 吴昊天<442384644@qq.com>
 * @day: 2020/7/8
 */
declare (strict_types=1);

namespace app\services\order;

use app\dao\order\TurntableLogDao;
use app\services\BaseServices;
use app\services\user\UserBillServices;
use app\services\user\UserServices;
use think\facade\Db;

/**
 *
 * Class StoreCartServices
 * @package app\services\order
 * @method updateCartStatus($cartIds) 修改购物车状态
 * @method getUserCartNum(int $uid, string $type, int $numType) 购物车数量
 * @method deleteCartStatus(array $cartIds) 修改购物车状态
 */
class TurntableLogServices extends BaseServices
{

    /**
     * StoreCartServices constructor.
     * @param TurntableLogDao $dao
     */
    public function __construct(TurntableLogDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList($where)
    {
        [$page, $limit] = $this->getPageValue();
        return $this->dao->getList($where, $page, $limit);
    }

    public function getCount($where)
    {
        return $this->dao->getListCount($where);
    }

    public function turntable(int $prizeId, int $uid, $balance)
    {
        /** @var UserBillServices $userBillServices */
        $userBillServices = app()->make(UserBillServices::class);
        $limitIntegral = 100;
        $userService = app()->make(UserServices::class);
        $uInfo = $userService->getUserInfo($uid);
        if ((int)$uInfo['points'] < $limitIntegral) {
            return false;
        }
        $turntableLog = [
            'uid' => $uid,
            'prize' => $prizeId,
            'order' => 'TU' . getRandNumber(16),
            'add_time' => time(),
            'mark' => '大转盘抽奖',
        ];
        //写入积分明细表
        $integralData = [
            'uid' => $uid,
            'link_id' => $prizeId,
            'type' => 30,
            'pm' => 0,
            'title' => '大转盘抽奖消耗积分',
            'number' => $limitIntegral,
            'balance' => (int)$uInfo['points'] - $limitIntegral,
            'mark' => '大转盘抽奖消耗' . $limitIntegral . '积分',
            'add_time' => time()
        ];
        $billData     = [
            'title'    => '大转盘中奖',
            'uid'      => $uid,
            'pm'       => 1,
            'add_time' => time(),
            'category' => 'now_money',
            'type'     => 'system_add',
            'number'   => $balance,
            'balance'  => $uInfo['now_money'] + $balance,
            'mark'     => '大转盘中奖' . $balance . '元'
        ];
        Db::startTrans();
        //更新用户积分
        $res[] = $turn = $this->dao->save($turntableLog);
        $billData['link_id'] = $turn['id'];
        $res[] = $userBillServices->addBillData($billData);
        $res[] = $userService->bcInc($uid, 'now_money', $balance, 'uid');
        $sql = "update eb_user set points=points-" . (int)$limitIntegral . " where uid={$uid}";
        $res[] = Db::execute($sql);
        $res[] = Db::table('eb_user_integral_log')->save($integralData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

}
